//
//  HLSongListView.m
//  HLMusicBox
//
//  Created by hulinhu on 2018/1/4.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

#import "HLSongListView.h"
#import "HLSongListTableCell.h"
#import "HLSongListModel.h"
#import "MusicPlayerManager.h"

#define kloopImageWHeight  44

@interface HLSongListView (){
    
    MusicPlayerManager *_musicPlayer;
    HLSongPlayManager *playMagager;
    UIImageView *modeImagV;
    UILabel *titlelabel;
}

@end

@implementation HLSongListView

- (instancetype)initWithFrame:(CGRect)frame
                  topbtnClick:(btnClick)btnClick {
    self = [super initWithFrame:frame];
    if (self) {
        
         playMagager = [HLSongPlayManager shareInstance];
        _musicPlayer = [MusicPlayerManager shareInstance];
        self.songListArray = playMagager.songListArray;
        
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.topHeardView = [GFControl createUIViewWithFrame:CGRectMake(0, 0, KScreenWidth, 40) bgColor:[UIColor lightGrayColor]];
        
        // 循环图片
        modeImagV = [GFControl createImageViewWithFrame:CGRectMake(10, (self.topHeardView.height-kloopImageWHeight)*0.5, kloopImageWHeight, kloopImageWHeight) imageName:@"cm2_icn_loop" cornerRadius:0 tapAction:^{
            [self playModeButtonAction];
        }];
        [self.topHeardView addSubview:modeImagV];
       
        // 显示标题
        NSString *title = [NSString stringWithFormat:@"循序播放(%ld)",self.songListArray.count];
        titlelabel = [GFControl createLabelWithFrame:CGRectMake(modeImagV.right+5, 0, KScreenWidth-modeImagV.right-10, self.topHeardView.height) text:title textAlignment:NSTextAlignmentLeft fontSize:20 textColor: UIColorHex(FFFFFF)];
        [self.topHeardView addSubview:titlelabel];
        self.tableHeaderView = self.topHeardView;
    }
    return self;
}


#pragma mark
#pragma mark - TableView Delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.songListArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseIdentifier = @"HLSongListTableCell";
    HLSongListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell == nil) {
        cell =  [[[NSBundle mainBundle] loadNibNamed:@"HLSongListTableCell" owner:self options:nil] lastObject] ;
    }
    HLSongListModel *model = self.songListArray[indexPath.row];
    cell.songName.text = model.title;
    cell.singerName.text = model.author;
    [cell.IconImage sd_setImageWithURL:[NSURL URLWithString:model.pic_small] placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HLSongListModel *info = (self.songListArray)[indexPath.row];
    NSLog(@"你选择了《%@》这首歌", info.title);
    HLSongPlayManager *playManager = [HLSongPlayManager shareInstance];
    [playManager requestCurrentPlaySong:info.song_id playSongIndex:indexPath.row];
}

-(void)playModeButtonAction {
    
    NSString *title = @"";
    switch (_musicPlayer.shuffleAndRepeatState) {
        case RepeatPlayMode: {
             modeImagV.image = [UIImage imageNamed:@"cm2_icn_one"];
            _musicPlayer.shuffleAndRepeatState = RepeatOnlyOnePlayMode;  //单曲循环
            title = [NSString stringWithFormat:@"单曲循环(%ld)",self.songListArray.count];
         }
            break;
        case RepeatOnlyOnePlayMode: {
            modeImagV.image = [UIImage imageNamed:@"cm2_icn_shuffle"];
            _musicPlayer.shuffleAndRepeatState = ShufflePlayMode;
            title = [NSString stringWithFormat:@"随机播放(%ld)",self.songListArray.count];
        }
            break;
        case ShufflePlayMode: {
             modeImagV.image = [UIImage imageNamed:@"cm2_icn_loop"];
            _musicPlayer.shuffleAndRepeatState = RepeatPlayMode;
            title = [NSString stringWithFormat:@"顺序播放(%ld)",self.songListArray.count];
        }
            break;
        default:
            break;
    }
    titlelabel.text = title;
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:_musicPlayer.shuffleAndRepeatState] forKey:@"SHFFLEANDREPEATSTATE"];//存储路径
}
-(void)getShuffleAndRepeatState{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSNumber *repeatAndShuffleNumber = [defaults objectForKey:@"SHFFLEANDREPEATSTATE"];
    if (repeatAndShuffleNumber == nil) {
        _musicPlayer.shuffleAndRepeatState = RepeatPlayMode;
    }
    else{
        _musicPlayer.shuffleAndRepeatState = (ShuffleAndRepeatState)[repeatAndShuffleNumber integerValue];
    }
    
    NSString *title = @"";

    switch (_musicPlayer.shuffleAndRepeatState) {
        case RepeatPlayMode:{
            modeImagV.image = [UIImage imageNamed:@"cm2_icn_loop"];
            title = [NSString stringWithFormat:@"顺序播放(%ld)",self.songListArray.count];

        }
            break;
        case RepeatOnlyOnePlayMode: {
            modeImagV.image = [UIImage imageNamed:@"cm2_icn_one"];
            title = [NSString stringWithFormat:@"单曲循环(%ld)",self.songListArray.count];
        }
            break;
        case ShufflePlayMode: {
            modeImagV.image = [UIImage imageNamed:@"cm2_icn_shuffle"];
            title = [NSString stringWithFormat:@"随机播放(%ld)",self.songListArray.count];
        }
              break;
        default:
            break;
    }
    titlelabel.text = title;

}

-(NSArray *)songListArray {
    if (!_songListArray) {
        _songListArray = [[NSArray alloc] init];
    }
    return _songListArray;
}

@end
